<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04.使用JSONP非同源服务请求数据</title>
  </head>

  <body>
    <button id="btn">点击发送JSONP请求</button>
  </body>
  <script>
    function fn2(data) {
      console.log("全局函数fn2");
      console.log(data);
    }
  </script>
  <script type="text/javascript">
    let btn = document.querySelector("#btn");
    btn.onclick = () => {
      // 1.创建script标签
      let script = document.createElement("script");
      // 2.获取body
      let body = document.body;
      // 3.设置script的src的值
      script.src = "http://localhost:3001/better?callback=fn2"
      // 4.在body里添加script标签
      body.appendChild(script);
      // 5.优化：删除script标签
      script.onload = () => {
        body.removeChild(script);
      }
    }
  </script>

</html>